package RecycleViewUtil;

import android.content.Context;
import android.databinding.Bindable;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.sunstart_library.R;

import java.util.ArrayList;
import java.util.List;

import Listener.ItemClickListener;
import SunStarUtils.UtilBox;

/**
 * @Title: ${file_name}
 * @Description: ${todo}<recycle通用adapter>
 * @data: 2016/9/6 15:51
 * @version: V1.0
 */
public abstract class SunStartBaseAdapter extends RecyclerView.Adapter<RVHolder>{
    public List<?> list;
    public Context context;
    public SunStartBaseAdapter(Context context, List<?> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(onCreateViewLayoutID(viewType), parent, false);
        return new RVHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
            return position;
    }

    public abstract int onCreateViewLayoutID(int viewType);


    @Override
    public void onViewRecycled(final RVHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onBindViewHolder(final RVHolder holder, final int position) {
        if(!(holder instanceof FooterViewHolder)){
            onBindViewHolder(holder.getViewHolder(), position);
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(null, v, holder.getPosition(), holder.getItemId());
                    }
                });
            }
            if(onItemLongClickListener!=null){
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        onItemLongClickListener.onItemLongClick(null, view, holder.getPosition(), holder.getItemId());
                        return true;
                    }
                });
            }
        }
    }

    public abstract void onBindViewHolder(ViewHolder holder, int position);

    @Override
    public int getItemCount() {
            return list.size();
    }

    public AdapterView.OnItemLongClickListener onItemLongClickListener;

    public AdapterView.OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class FooterViewHolder extends RVHolder {

        public FooterViewHolder(View view) {
            super(view);
        }

    }

}
